﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZXingBarCodeDemo.Forms;

namespace ZXingBarCodeDemo
{
    public partial class FrmMain : Form
    {
        /// <summary>
        /// logo的路径地址
        /// </summary>
        private string logPath = string.Empty; //AppDomain.CurrentDomain.BaseDirectory + "Images\\qr_logo.jpg";
        /// <summary>
        ///原创信息窗体
        /// </summary>
        FrmWeiXin frmWeiXin = new FrmWeiXin();
        /// <summary>
        ///构造函数
        /// </summary>
        public FrmMain()
        {
            InitializeComponent();
            frmWeiXin.Show();
        }

        /// <summary>
        /// 选择logo
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_selectlogo_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);//初始路径为桌面
                openFileDialog.Filter = "Logo图片|*.png;*.jpg;*.jpeg;*.ico";
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    picLogo.Image = Image.FromFile(openFileDialog.FileName);
                    logPath = openFileDialog.FileName;//logo文件路径
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_generate_Click(object sender, EventArgs e)
        {
            try
            {
                string errorMsg = "";
                string content = rtbQRCodeContent.Text;
                if (string.IsNullOrWhiteSpace(content))
                {
                    MessageBox.Show("二维码内容不能为空！");
                    return;
                }

                picQRCode.Image = ZXingHelper.GenerateQRCode(content, out errorMsg, logPath);
                if (!string.IsNullOrWhiteSpace(errorMsg))
                {
                    MessageBox.Show(errorMsg);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 保存二维码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_save_Click(object sender, EventArgs e)
        {
            try
            {
                if (picQRCode.Image == null)
                {
                    MessageBox.Show("请先生成二维码！");
                    return;
                }
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "保存图片|*.png;*.jpg;*.jpeg";
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    picQRCode.Image.Save(saveFileDialog.FileName, ImageFormat.Png);
                    MessageBox.Show("保存成功！");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 识别二维码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_identityQrCode_Click(object sender, EventArgs e)
        {
            try
            {
                if (picQRCode.Image == null)
                {
                    MessageBox.Show("请先生成二维码！");
                    return;
                }
                Bitmap Imagemap = new Bitmap(picQRCode.Image);
                string QRCodeResult = ZXingHelper.ParseBarCode(Imagemap);
                rtbQRCodeResult.Text = QRCodeResult;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 打开原创二维码信息页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btNotes_Click(object sender, EventArgs e)
        {
            try
            {
                if (frmWeiXin.WindowState == FormWindowState.Minimized)
                {
                    frmWeiXin.WindowState = FormWindowState.Normal;
                }
                else
                {
                    if (frmWeiXin.IsDisposed)
                    {
                        frmWeiXin = new FrmWeiXin();
                    }
                 
                }
                frmWeiXin.StartPosition = FormStartPosition.WindowsDefaultLocation;
                frmWeiXin.Activate();
                frmWeiXin.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
         
        }
    }
}
